<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Shenhe extends Common
{

	//反馈列表
    public function index()
    {

        $where = [];
        $where['t.state'] = ['in','1,3,4'];//发起
        $where['t.type'] = 1;//非常规
        $where['t.is_deleted'] = 0;//非常规
        $where['t.is_actived'] = 1;//非常规
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        if($sn){
            $where['sn'] = ['like',"%$sn%"];
        }
        $title = $this->request->get('title');
        $this->assign('title',$title);
        if($title){
            $where['t2.title'] = ['like',"%$title%"];
        }

        $count = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->where($where)->count();
        $data = Db::table('all_ti_feeback_list t')
            ->join('cg_ti t2','t2.id=t.apply_id')
            ->order("t.state asc")
            ->where($where)
            ->field('t.*,t2.title,t2.cate_id')
            ->paginate(10,$count);

//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['c_user_id'] = Db::table('employee')->where(array('id'=>$v['c_user_id']))->value('em_name');
            $data[$k]['cate_title'] = Db::table('cg_ti_cate')->where(array('id'=>$v['cate_id']))->value('title');

        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    /**
     * 反馈审核
     * @throws \think\Exception
     */
    public function shenhe(){
        $id = $this->request->param('id');
        $state = $this->request->param('state');
        if(!$id||!$state){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }

        $bool = Db::table('all_ti_feeback_list')
            ->where(array('id'=>$id))
            ->update([
                'state'=>$state
            ]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>($state==3?"通过":"打回")."成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>($state==3?"通过":"打回")."失败"
            ]);die;
        }
    }

	//原单列表
    public function indexyd()
    {

        $where = [];
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        if($sn){
            $where['sn'] = ['like',"%$sn%"];
        }
        $title = $this->request->get('title');
        $this->assign('title',$title);
        if($title){
            $where['t2.title'] = ['like',"%$title%"];
        }

        $count = Db::table('cg_ti t')
            ->where($where)->count();
        $data = Db::table('cg_ti t')
            ->order("t.id desc")
            ->where($where)
            ->paginate(10,$count);

//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cuser'] = Db::table('employee')->where(array('id'=>$v['cuser']))->value('em_name');
            $data[$k]['cate_title'] = Db::table('cg_ti_cate')->where(array('id'=>$v['cate_id']))->value('title');

        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    /**
     * 原单审核
     * @throws \think\Exception
     */
    public function shenheyd(){
        $id = $this->request->param('id');
        $state = $this->request->param('state');
        if(!$id||!$state){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }

        //查询申请类型,如果是取消，则生成一个状态为7的取消后剩余明细的申请单，以供以后反馈和核销
        $cate_id = db('cg_ti')->where('id',$id)->value('cate_id');
        if($cate_id==3){
            //查询原单id
            $qx_list = db('cg_ti_list')->where('ti_id',$id)->column('refer_id');
            $y_ti_id = db('cg_ti')->where('id',$id)->value('old');
            //查询原始单据明细
            $y_list = db('cg_ti_list')->where('ti_id',$y_ti_id)->column('id');
            $y_list = array_diff($y_list,$qx_list);
            if($y_list!=[]){ //生成新的单据
                $row = db('cg_ti')->where('id',$id)->field('title,arealat,productlat,jxs_id')->find();
                $insert_sn = $this->produceSN();//生成编号
                $new_ti_id = db('cg_ti')->insertGetId([
                    'sn'=>$insert_sn,
                    'title'=>$row['title'],
                    'arealat'=>$row['arealat'],
                    'productlat'=>$row['productlat'],
                    'jxs_id'=>$row['jxs_id'],
                    'ctime'=>time(),
                    'ptime'=>time(),
                    'cuser'=>Session::get('employee_id'),
                    'puser'=>Session::get('employee_id'),
                    'status'=>7,
                ]);
                $insert = db('cg_ti_list')->where('id','in',$y_list)->select();
                foreach($insert as $k=>$v){
                    $v['ti_id'] = $new_ti_id;
                    $v['cuser'] = session('employee_id');
                    $v['puser'] = session('employee_id');
                    $v['ctime'] = time();
                    $v['ptime'] = time();
                    $v['status'] = 0;
                    $v['id'] = null;
                    $insert[$k] = $v;
                }
                db('cg_ti_list')->insertAll($insert);
            }
        }

        $bool = Db::table('cg_ti')
            ->where(array('id'=>$id))
            ->update([
                'status'=>$state
            ]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>($state==7?"通过":"打回")."成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>($state==7?"通过":"打回")."失败"
            ]);die;
        }
    }

	//核销列表
    public function indexhx()
    {

        $where = [];
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        if($sn){
            $where['sn'] = ['like',"%$sn%"];
        }
        $title = $this->request->get('title');
        $this->assign('title',$title);
        if($title){
            $where['t2.title'] = ['like',"%$title%"];
        }

        $count = Db::table('cg_ti_hexiao t')
            ->where($where)->count();
        $data = Db::table('cg_ti_hexiao t')
            ->order("t.id desc")
            ->where($where)
            ->paginate(10,$count);

//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cuser'] = Db::table('employee')->where(array('id'=>$v['cuser']))->value('em_name');

        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }

    /**
     * 核销审核
     * @throws \think\Exception
     */
    public function shenhehx(){
        $id = $this->request->param('id');
        $state = $this->request->param('state');
        if(!$id||!$state){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }

        $bool = Db::table('cg_ti_hexiao')
            ->where(array('id'=>$id))
            ->update([
                'status'=>$state
            ]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>($state==7?"通过":"打回")."成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>($state==7?"通过":"打回")."失败"
            ]);die;
        }
    }

    //返回二维数组中的某二维key的合计
    public function array_sum($arr,$key){
        $sum = 0;
        foreach($arr as $k=>$v){
            $sum += $v[$key];
        }
        return $sum;
    }

    public function produceSN(){
        //生成编号
        $max = db('cg_ti')->where('sn','like',date('Ymd').'%')->max('sn'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }
}
